/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public final class cJaccard {

    public static void cjaccard(DataPreparation data) throws FileNotFoundException, IOException, WeiboException, InterruptedException {

        String uid = Config.uid;
        String path = Config.path;
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + "1.txt")));
        List<String> bfID = data.data.mBiFIDs;
        Map<String, String> linkout = data.LinkOut;
        Map<String, String> linkin = data.LinkIn;
        List<String> fID = data.data.nodes;
        Map<String, Double> myFP = new HashMap<String, Double>();
        Map<String, String> id2name = data.data.username;
        double s1 = 1;//我单向关注一个共同好友、好友单向关注一个共同好友
        double s2 = 10;//我的互粉，是我们的共同好友
        double s3 = 1;//他的单向关注一个共同好友
        double s4 = 8;//他的互粉，是我们的共同好友
        double s5 = 2;//不是共同好友，我单向关注
        double s6 = 4;//不是共同好友，我双向关注
        int alpha = 5;
        int beta = 3;
        int gamma=30;
        commentData.commentData(data);
        repostData.repostData(data);
        for (String user : fID) {
            data.jaccard.put(user, 0.0);
        }
        //我的所有好友的评分
        for (String user : fID) {
            if (user.equals("0") || user.equals(uid)) {
                myFP.put(user, 0.0);
            }
            if (bfID.contains(user)) {
                myFP.put(user, s2);
                if (data.data.comment.containsKey(user)) {
                    myFP.put(user, s2 + (data.data.comment.get(user) * alpha));
                    if (data.data.repost.containsKey(user)) {
                        myFP.put(user, s2 + (data.data.comment.get(user) * alpha) + (data.data.repost.get(user) * beta));
                    }
                }
            } else {
                myFP.put(user, s1);
                if (data.data.comment.containsKey(user)) {
                    myFP.put(user, s1 + (data.data.comment.get(user) * alpha));
                    if (data.data.repost.containsKey(user)) {
                        myFP.put(user, s1 + (data.data.comment.get(user) * alpha) + (data.data.repost.get(user) * beta));
                    }
                }
            }
            if(data.data.latest.containsKey(user)){
                myFP.put(user, myFP.get(user)+data.data.latest.get(user)*gamma);
            }
        }
        //他的评分
        for (String s : bfID) {
            if (s.equals("0")) {
                continue;
            }
            String bfOut[] = linkout.get(s).split(" ");
            int bfOutSize = bfOut.length;
            List<String> bfOutList = new ArrayList<String>();
            int bfCount = 0;
            for (int i = 0; i < bfOutSize; i++) {
                if (!bfOut[i].equals("0") && !bfOut[i].equals(uid)) {
                    bfOutList.add(bfOut[i]);
                    bfCount += 1;
                }
            }
            data.data.biFCount.put(s, bfCount);
            double combine = 0;
            for (String user : bfOutList) {
                String bfIn[] = linkin.get(user).split(" ");
                int bfInSize = bfIn.length;
                List<String> bfInList = new ArrayList<String>();
                for (int i = 0; i < bfInSize; i++) {
                    if (!bfIn[i].equals("0") && !bfIn[i].equals(uid)) {
                        bfInList.add(bfIn[i]);
                    }
                }

                double myscore = myFP.get(user);

                double hisscore = 0;
                if (bfInList.contains(s)) {
                    hisscore = s4;
                } else {
                    hisscore = s3;
                }
                combine += (hisscore + myscore);
            }
            if (data.data.comment.containsKey(s)) {
                if (data.data.repost.containsKey(s)) {
                    combine += (myFP.get(s) * data.data.comment.get(s) * alpha + myFP.get(s) * data.data.repost.get(s) * beta);
                } else {
                    combine += (myFP.get(s) * data.data.comment.get(s) * alpha);
                }
            }else{
                if (data.data.repost.containsKey(s)) {
                    combine += (myFP.get(s) * data.data.repost.get(s) * beta);
                } 
            }
            combine = combine / 2.0;
            double union = 0;
            for (String user : fID) {
                if (!bfOutList.contains(user) && !user.equals(s)) {
                    union += myFP.get(user);
                }
            }
            union += combine;
            double score = combine / union;
            data.jaccard.put(s, score);
            //System.out.println(id2name.get(s) + " " + score);
        }
        writer.close();
    }
}
